class Solution {

public:
    vector<vector<int>> ret;
    vector<int> path;
    vector<int> vis;
    int n;
    void dfs(vector<int>& nums) {
        if (path.size() == n) {
            ret.push_back(path);
            return;
        }
        for (int i = 0; i < n; i++) {
            if (!vis[i]) {
                if (i>0 && nums[i] == nums[i - 1] && !vis[i - 1]) {
                    continue;
                }
                vis[i] = 1;
                path.push_back(nums[i]);
                dfs(nums);
                path.pop_back();
                vis[i] = 0;
            }
        }
    }
    vector<vector<int>> permuteUnique(vector<int>& nums) {
        n = nums.size();
        vis.resize(n, 0);
        sort(nums.begin(), nums.end());
        dfs(nums);
        return ret;
    }
};
